class CauHinh
  include MongoMapper::Document

  key     :ten,              String,   :presence => true,    :length => {:minimum => 4}, :uniqueness => true
  key     :gia_tri,          String,   :presence => true
  timestamps!
  
  def self.gia_tri(ten, mac_dinh)
    cau_hinh = CauHinh.find_by_ten(ten)
    if cau_hinh.present?
      return cau_hinh.gia_tri
    else
      cau_hinh = CauHinh.create({:ten => ten, :gia_tri => mac_dinh})
      return mac_dinh
    end
  end
  
  def self.lay_gia_tri_hoac_khoi_tao(ten, mac_dinh)
    cau_hinh = CauHinh.find_by_ten(ten)
    if !cau_hinh.present?
      cau_hinh = CauHinh.create({:ten => ten, :gia_tri => mac_dinh})
    end
    return cau_hinh
  end
  
end
